//
//  Player_preWaveform.m
//  SoundPill
//
//  Created by 宋旭东 on 15/4/22.
//  Copyright (c) 2015年 宋旭东. All rights reserved.
//

#import "Player_preWaveform.h"

@interface Player_preWaveform ()
{
    NSInteger index;
}
@end

@implementation Player_preWaveform
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.preWaveArray = [[NSArray alloc] init];
    }
    return self;
}
- (void)getAudioPresses:(float)presses
{
    if (_preWaveArray.count > 0) {
        index = _preWaveArray.count * presses;
        [self setNeedsDisplay];
    }
//    if (index < _preWaveArray.count) {
//    }
}

// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
//     Drawing code
    float offset = [UIScreen mainScreen].bounds.size.width / 2;
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetLineCap(context, kCGLineCapSquare); //线条样式
    CGContextSetLineWidth(context, 3); //线条宽度

    CGContextSetRGBStrokeColor(context, 0.898f, 0.902f, 0.906f, 1);
    CGContextBeginPath(context);
    for (int i = 0; i < index; i++) {
        offset+=6;
        NSString *str = _preWaveArray[i];
        if (i == index - 1) {
            CGContextMoveToPoint(context, offset, self.bounds.size.height / 2 + str.floatValue * 0.15);
            CGContextAddLineToPoint(context, offset, self.frame.size.height / 2 + 2);
        } else if (i == index - 2) {
            CGContextMoveToPoint(context, offset, self.bounds.size.height / 2 + str.floatValue * 0.25);
            CGContextAddLineToPoint(context, offset, self.frame.size.height / 2 + 2);
        }  else if (i == index - 3) {
            CGContextMoveToPoint(context, offset, self.bounds.size.height / 2 + str.floatValue * 0.35);
            CGContextAddLineToPoint(context, offset, self.frame.size.height / 2 + 2);
        } else if (i == index - 4) {
            CGContextMoveToPoint(context, offset, self.bounds.size.height / 2 + str.floatValue * 0.45);
            CGContextAddLineToPoint(context, offset, self.frame.size.height / 2 + 2);
        } else if (i == index - 5) {
            CGContextMoveToPoint(context, offset, self.bounds.size.height / 2 + str.floatValue * 0.55);
            CGContextAddLineToPoint(context, offset, self.frame.size.height / 2 + 2);
        } else if (i == index - 6) {
            CGContextMoveToPoint(context, offset, self.bounds.size.height / 2 + str.floatValue * 0.65);
            CGContextAddLineToPoint(context, offset, self.frame.size.height / 2 + 2);
        }  else if (i == index - 7) {
            CGContextMoveToPoint(context, offset, self.bounds.size.height / 2 + str.floatValue * 0.75);
            CGContextAddLineToPoint(context, offset, self.frame.size.height / 2 + 2);
        } else {
        CGContextMoveToPoint(context, offset, self.bounds.size.height / 2 + str.floatValue * 0.8);
        CGContextAddLineToPoint(context, offset, self.frame.size.height / 2 + 2);
        }
    }
    CGContextStrokePath(context);
    _lastOffset = offset;
}


@end
